MIPS assembly continued

Memory

Arrays:

Example 1: Array of objects

// in memory, this will look the same
// as an array of length 2
class C {
    int num1;
    int num2;
} o1;

// so this will be like an array
// of length 6
objs[3];

In this class we don't require thinking about struct alignment on exams or homeworks.

Class methods:

Unsigned notation

In unsigned notation, we write 12 base 10 as 0000 1100 in binary.

In unsigned notation, we pad numbers with zeros regardless of sign.

However, in signed (2's complement) notation we pad negative numbers with ones.

2's complement will appear on the exam.

lbu and lhu instructions

lbu stands for "Load Byte Unsigned"

Similarly, lhu stands for "load half-word unsigned", and behaves similarly except it reads 2 bytes instead of one.

There is no "lwu" instruction:

Example of lb instruction with negative number

We do not need unsigned "store" instructions:

Common exam question - "give -65 in 16-bit 2's complement notation"

Since -1 is 1111 1111 1111 1111 in 2's complement notation, we just need to subtract 64 from this to get the desired result. 64 is 2^5, so we get 1111 1111 1011 1111 (note that we start counting from the right side at 2^0).

2's complement is basically just taking all the numbers up to the maximum number given by the range (all 1's) and then slicing off the top half and calling them negative numbers. The top half is sliced off and placed in front of the range starting at 0, so all 1's corresponds to -1. In order to write -x, we just need to find what the binary version of +x is and then subtract that from the maximum integer (all 1's) but then add 1 back, because all 1's represents -1.

(Note that negative numbers necessarily cannot be expressed in unsigned notation.)

Endianness - in what order bytes are loaded into the register

Two approaches - "big-endian" and "little-endian"

Say we have the following in RAM, where B1..B4 represent bytes 1 through 4:

contents|address -|- B1|8 B2|9 B3|A B4|B

And we have the following instruction in MIPS:

lw $s0, -2($s7)

Assume s7 holds 10 (A in hex), so -2($s7) means 8

Here is what is in s0:

By default our class uses the big-endian approach.

Regardless of endianness, we still start filling the register from the right side: